<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { UserOutlined, LockOutlined, MailOutlined } from '@ant-design/icons-vue'
import { useUserStore } from '@/stores/user'
import type { RegisterData } from '@/api/user'

const router = useRouter()
const userStore = useUserStore()

const loading = ref(false)
const formData = reactive<RegisterData>({
  username: '',
  password: '',
  confirmPassword: '',
  email: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: async (_rule: any, value: string) => {
        if (value !== formData.password) {
          throw new Error('两次输入的密码不一致')
        }
      },
      trigger: 'blur'
    }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ]
}

const handleSubmit = async () => {
  try {
    loading.value = true
    const success = await userStore.register(formData)
    if (success) {
      message.success('注册成功,请登录')
      router.push('/login')
    }
  } finally {
    loading.value = false
  }
}

const goToLogin = () => {
  router.push('/login')
}
</script>

<template>
  <div class="register-container">
    <div class="register-background">
      <div class="shape"></div>
      <div class="shape"></div>
    </div>
    <div class="register-box">
      <div class="register-header">
        <h2>创建账号</h2>
        <p>加入我们的社区</p>
      </div>
      <a-form
        :model="formData"
        :rules="rules"
        @finish="handleSubmit"
        class="register-form"
      >
        <a-form-item name="username">
          <a-input
            v-model:value="formData.username"
            placeholder="请输入用户名"
            size="large"
            class="custom-input"
          >
            <template #prefix>
              <UserOutlined class="input-icon" />
            </template>
          </a-input>
        </a-form-item>
        
        <a-form-item name="password">
          <a-input-password
            v-model:value="formData.password"
            placeholder="请输入密码"
            size="large"
            class="custom-input"
          >
            <template #prefix>
              <LockOutlined class="input-icon" />
            </template>
          </a-input-password>
        </a-form-item>

        <a-form-item name="confirmPassword">
          <a-input-password
            v-model:value="formData.confirmPassword"
            placeholder="请确认密码"
            size="large"
            class="custom-input"
          >
            <template #prefix>
              <LockOutlined class="input-icon" />
            </template>
          </a-input-password>
        </a-form-item>

        <a-form-item name="email">
          <a-input
            v-model:value="formData.email"
            placeholder="请输入邮箱"
            size="large"
            class="custom-input"
          >
            <template #prefix>
              <MailOutlined class="input-icon" />
            </template>
          </a-input>
        </a-form-item>

        <a-form-item>
          <a-button
            type="primary"
            html-type="submit"
            :loading="loading"
            block
            size="large"
            class="submit-button"
          >
            注册
          </a-button>
        </a-form-item>

        <div class="register-form-login">
          <span>已有账号?</span>
          <a @click="goToLogin" class="login-link">立即登录</a>
        </div>
      </a-form>
    </div>
  </div>
</template>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(45deg, #4ECDC4, #FF6B6B);
  position: relative;
  overflow: hidden;
}

.register-background {
  position: absolute;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.shape {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(5px);
  animation: float 10s infinite ease-in-out;
}

.shape:nth-child(1) {
  width: 500px;
  height: 500px;
  top: -250px;
  left: -100px;
  animation-delay: 0s;
}

.shape:nth-child(2) {
  width: 400px;
  height: 400px;
  bottom: -200px;
  right: -100px;
  animation-delay: -5s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(-10deg);
  }
}

.register-box {
  width: 100%;
  max-width: 420px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  z-index: 2;
  transform: translateY(0);
  transition: transform 0.3s ease;
}

.register-box:hover {
  transform: translateY(-5px);
}

.register-header {
  text-align: center;
  margin-bottom: 40px;
}

.register-header h2 {
  color: #1a1a1a;
  font-size: 28px;
  margin-bottom: 8px;
}

.register-header p {
  color: #666;
  font-size: 16px;
}

.register-form {
  margin-top: 20px;
}

.custom-input {
  border-radius: 8px;
  border: 1px solid #e8e8e8;
  transition: all 0.3s ease;
}

.custom-input:hover,
.custom-input:focus {
  border-color: #FF6B6B;
  box-shadow: 0 0 0 2px rgba(255, 107, 107, 0.2);
}

.input-icon {
  color: #666;
}

.submit-button {
  height: 44px;
  font-size: 16px;
  border-radius: 8px;
  background: linear-gradient(45deg, #FF6B6B, #FF5252);
  border: none;
  box-shadow: 0 4px 12px rgba(255, 107, 107, 0.3);
  transition: all 0.3s ease;
}

.submit-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(255, 107, 107, 0.4);
  background: linear-gradient(45deg, #FF5252, #FF6B6B);
}

.register-form-login {
  text-align: center;
  margin-top: 24px;
}

.register-form-login span {
  color: #666;
}

.login-link {
  color: #FF6B6B;
  margin-left: 8px;
  font-weight: 500;
  transition: color 0.3s ease;
}

.login-link:hover {
  color: #4ECDC4;
}

/* 响应式设计 */
@media (max-width: 576px) {
  .register-box {
    margin: 20px;
    padding: 30px;
  }

  .register-header h2 {
    font-size: 24px;
  }

  .shape {
    display: none;
  }
}
</style> 